package m.store.action;

import m.base.action.BaseAction;
import m.base.model.GoodsInfo;
import m.base.model.StoreInfo;
import m.base.service.GoodsInfoService;
import m.base.util.PowerUtil;
import m.base.util.ZolUtil;
import m.common.action.ActionMeta;
import m.common.model.util.ModelQueryList;
import m.common.model.util.ModelUpdateUtil;
import m.common.model.util.QueryCondition;
import m.store.model.StoreGoods;
import m.store.model.StoreType;
import m.store.service.StoreGoodsService;
import m.system.db.TransactionManager;
import m.system.exception.MException;
import m.system.util.JSONMessage;
import m.system.util.StringUtil;
import manage.model.StatusModel;

import java.util.List;

@ActionMeta(name = "store/storeGoodsZol")
public class StoreGoodsZolAction extends BaseAction {

    @Override
    protected JSONMessage processImportSetting(String importParam) {
        return new JSONMessage()
            .push("alertMessage","根据中关村商品详情页面链接提取商品信息，创建商品并补充商品信息。")
            .push("tempPath","page/store/中关村商品信息导入.xlsx")
            .push("tempName","中关村商品信息导入")
            .push("canStop",true);
    }
    @Override
    protected String processImportData(List<String[]> data, String importParam) throws Exception {
        PowerUtil.verifyPower(PowerUtil.Store);
        setImportCurrentTitle("正在处理商品信息");
        String msg;
        TransactionManager tm=new TransactionManager();
        try{
            StoreInfo store=PowerUtil.getSessionStore(true);
            tm.begin();
            int n=0;
            StringBuffer error=new StringBuffer();
            setImportCountNum(data.size());
            for(int i=0;i<data.size();i++) {//循环数据，已排除第一行
                setImportCurrentNum(i);
                if(isStopImport()) throw new MException(this.getClass(),"用户中止导入\r\n"+error);
                String[] arr=parseRowData(data.get(i),5);//获取每行数据
                if(!StringUtil.isSpace(arr[0])){
                    int row=i+2;
                    try{
                        StoreType type=null;
                        if(StringUtil.notSpace(arr[1])) {
                            type = ModelQueryList.getModel(StoreType.class, new String[]{"*"},
                                QueryCondition.and(
                                    QueryCondition.eq("name", arr[1]),
                                    QueryCondition.eq("store.oid", PowerUtil.getSessionStoreOid())
                                )
                            );
                            if (null == type) throw new MException(this.getClass(), arr[1] + " 分类不存在");
                        }
                        GoodsInfo model = getService(GoodsInfoService.class).doSave(ZolUtil.getOutId(arr[0]));
                        if(!StatusModel.Status_Enable.equals(model.getStatus())) {//不是启用状态再次采集
                            model = getService(GoodsInfoService.class).doCollection(model.getOid());
                        }
                        StoreGoods sg=getService(StoreGoodsService.class).copyGoodsInfo(store,model);
                        sg.setStoreType(type);
                        sg.setOfferPrice(parseDouble(arr[2],row,"报价",error));
                        ModelUpdateUtil.updateModel(sg,"offerPrice","storeType.oid");
                        n++;
                    }catch(Exception e){
                        error.append("\r\n第").append(row).append("行!").append(e.getMessage());
                        setImportMessage(error.toString());
                    }
                }
            }
            if(error.length()>0){
                throw new MException(this.getClass(),error.toString());
            }
            msg="上传了"+data.size()+"条，成功导入了"+n+"条";
            tm.commit();
        }catch (Exception e){
            tm.rollback();
            throw e;
        }
        return msg;
    }
}
